home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 554 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: jbuck@Synopsys.COM (Joe Buck)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: The realloc question: rationale?
  5. Date: 26 Feb 1996 20:13:36 GMT
  6. Organization: Synopsys Inc., Mountain View, CA 94043-4033
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4gsupf$ovm@hermes.synopsys.com>
  9. References: <4g903m$7g8@mari.onr.com> <4gl2ad$lqi@hermes.synopsys.com> <31301BFE.450A@onr.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. X-Nntp-Posting-Host: deerslayer.synopsys.com
  12. Originator: clamage@taumet
  13.  
  14.  
  15. I wrote:
  16. >> You can, of course, call the C realloc().
  17.  
  18. Kerry Kimbrough <kk@onr.com> writes:
  19. >But then crash? My understanding is that mixture of alloc/free and friends 
  20. >with new/delete is not guaranteed to be valid and therefore is discouraged. 
  21. >Not true?
  22.  
  23. True.  If you want to use the C realloc, you'll need to mix it with malloc
  24. and free.
  25.  
  26. >If true, then realloc'ing a new'ed object ain't kosher, and there goes the 
  27. >hope for specializing STL to implement realloc efficiently, i.e. w/o copy and 
  28. >delete.
  29.  
  30. That does not follow: my discussion of how to do the equivalent of realloc
  31. using template specialization does not use the realloc function at all.
  32. STL (at least in the HP implementation) does not use new directly to get
  33. memory; allocators get raw memory, and placement new is used to turn raw
  34. memory into objects.  STL already has to have a realloc mechanism: when
  35. vectors and deques grow, they are reallocated and the older data is moved
  36. to fit into the newly allocated space.  This is exactly what realloc does.
  37. So answer #1 to those who cry out for realloc is to say "Use the STL
  38. vector class: it looks like an array and does realloc for you".  For
  39. arrays of T where T doesn't have a destructor, it's just the same.
  40. To reallocate a vector to have capacity n, you just say
  41.  
  42.     myVector.reserve(n);
  43.  
  44. The HP implementation does the reallocation operation by calling
  45. uninitialized_copy (using the copy constructor and placement new to
  46. generate copies) followed by destroy (invoking the destructor on the
  47. original objects).  This is inefficient for class objects that have lots
  48. of sub-objects on the heap, as a shallow copy would be a lot less work.
  49.  
  50. My proposed modification is to replace the uninitialized_copy/destroy
  51. combination with a "relocate" template that, by default, does the same
  52. thing, but that can be specialized to move specific classes more
  53. efficiently.  But all that does is make relocation work better; it's
  54. already there now.
  55.  
  56.  
  57.  
  58.  
  59.  
  60. -- 
  61. -- Joe Buck     <jbuck@synopsys.com>    (not speaking for Synopsys, Inc)
  62.  
  63. Work for something because it is good,
  64. not just because it stands a chance to succeed.       -- Vaclav Havel
  65.  
  66.  
  67. [ To submit articles: Try just posting with your newsreader.
  68.               If that fails, use mailto:std-c++@ncar.ucar.edu
  69.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  70.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  71.   Comments? mailto:std-c++-request@ncar.ucar.edu
  72. ]
  73.